package com.mygdx.game.Screens;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.B2WorldCreator;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Scenes.ActiveCharacter;
import com.mygdx.game.Scenes.Hud;
import com.mygdx.game.Sprite.NPC;
import com.mygdx.game.Sprite.Mario;

import com.mygdx.game.Stage.Fight.ExampleBasicFight;
import com.mygdx.game.Stage.GeneralStage;
import com.mygdx.game.Stage.IndexStage;
import org.graalvm.compiler.phases.common.NodeCounterPhase;


public class PlayScreen implements Screen {
    public IndexStage indexStage;
//    public StartStage startStage;
    public GeneralStage generalStage;
    private TextureAtlas atlas;
    public static int positionFlag = 0;
    private ExampleBasicFight exampleBasicFight;

    public static MyGdxGame game;
    public static int flag = 0;
    public static Mario mario;
    public static NPC npcM;
    private Stage stage;
    private ActiveCharacter activeCharacter;

    private OrthographicCamera gamecam;
    private Viewport gamePort;
    private Hud hud;

    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;

    private boolean zhuang = false;


    //Box2d
    private World world;
    private Box2DDebugRenderer b2dr;
    private float timer;
    // 纹理画布
    private Sound sound;
    public PlayScreen(final MyGdxGame game) {
        this.game = game;
        zhuang = false;
        stage = new Stage();
        exampleBasicFight = new ExampleBasicFight();

       // activeCharacter = new ActiveCharacter("Character/GreenNPC",MyGdxGame.V_WIDTH/2,MyGdxGame.V_HEIGHT/2);



        atlas = new TextureAtlas("Mario_and_Enemies.pack");

        indexStage = new IndexStage();
        generalStage = new GeneralStage("image/background.png");

        // 加载音效, 创建 Sound 实例
        sound = Gdx.audio.newSound(Gdx.files.internal("music/click.mp3"));


        gamecam = new OrthographicCamera();
        gamePort = new FitViewport(MyGdxGame.V_WIDTH/3 , MyGdxGame.V_HEIGHT/3 , gamecam);
        hud = new Hud(game.batch,0,0);
        hud = new Hud(game.batch,910,495);
       // hud.stage.setViewport(gamePort);
     //   stage.addActor(activeCharacter);
        stage.setViewport(gamePort);


        mapLoader = new TmxMapLoader();
        map = mapLoader.load("MAP.tmx");

        renderer = new OrthogonalTiledMapRenderer(map,1);
        //gamecam.position.set(gamePort.getScreenWidth()/2,gamePort.getScreenHeight()/2,0);
        gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);


        world = new World(new Vector2(0, 0), true);

        b2dr = new Box2DDebugRenderer();

        new B2WorldCreator(world,map,game);
        if(positionFlag==0) {
            mario = new Mario(world, 5, 326, 400);
        }
        if (positionFlag==1){
            mario = new Mario(world, 5, 270, 900);
        }
        npcM = new NPC(world,6,324,431);
        world.setContactListener(new ContactListener() {
            @Override
            public void beginContact(Contact contact) {
                System.out.println(PlayScreen.mario.b2Body.getPosition());
                zhuang = true;

                if(PlayScreen.flag==1 && PlayScreen.mario.b2Body.getPosition().x>302.0 && PlayScreen.mario.b2Body.getPosition().x<347.0
                        && PlayScreen.mario.b2Body.getPosition().y>412 && PlayScreen.mario.b2Body.getPosition().y<450)
                {
                    Boss1Screen.positionFlag=0;
                    game.setScreen(new Boss1Screen(game));
                    System.out.println("GreenNPC");
                }

            }
            @Override
            public void endContact(Contact contact) {

            }
            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }
            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {

            }
        });

        // 设置日志输出级别
        Gdx.app.setLogLevel(Application.LOG_DEBUG);


    }
    public TextureAtlas getAtlas(){
        return atlas;
    }
    public World getWorld(){
        return world;
    }
    @Override
    public void show() {

    }

    public void update(float dt) {
        mario.handleInput(dt);
        world.step(1 / 60f, 6, 2);
        mario.update(dt);
        npcM.update(dt);
        //摄像头跟随人物底层图形
        gamecam.position.x = mario.b2Body.getPosition().x;
        gamecam.position.y = mario.b2Body.getPosition().y;
        gamecam.update();
        renderer.setView(gamecam);
    }

    @Override
    public void render(float delta) {


        if (Gdx.input.justTouched()) {
            // 点击屏幕一次, 播放音效一次
            sound.play();
        }

        timer = timer += Gdx.graphics.getDeltaTime();
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        //在b2dr下方就可以消失框
        renderer.render();
        b2dr.render(world, gamecam.combined);

        game.batch.setProjectionMatrix(gamecam.combined);
        game.batch.begin();
        mario.draw(game.batch);
        npcM.draw(game.batch);
        game.batch.end();

        GoHome();
        SelectStage();
        if (Gdx.input.isKeyPressed(Input.Keys.P)) {
       flag=2;
        }

    }

    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        map.dispose();
        renderer.dispose();
        world.dispose();
        b2dr.dispose();
        hud.dispose();
        indexStage.dispose();

    }

    public void SelectStage() {
        switch (flag) {
            case 0: {
                //index
                Gdx.input.setInputProcessor(indexStage);
                indexStage.draw();
            }
            break;

            case 1: {
                //start
//                Gdx.input.setInputProcessor(startStage);
//                startStage.draw();
//                startStage.Check();
                Gdx.input.setInputProcessor(hud.stage);

                //hud.activeCharacter.x=910;
                //hud.activeCharacter.y=495;
                //stage.draw();
//                hud.stage.draw();

            }
            break;
            case 2: {
                Gdx.input.setInputProcessor(exampleBasicFight);
            exampleBasicFight.draw();

            }
            break;
//
//            case 3: {
//                //attack
//                Gdx.input.setInputProcessor(attackStage1);
//                attackStage1.act();
//
//            }
//            break;

        }
        if (Gdx.input.isKeyPressed(Input.Keys.BACKSPACE)) {
            switch (flag) {
                case 1: {
                    flag = 0;
                }
                break;
                case 2: {
                    flag = 1;
                }
                break;
                case 3: {
                    flag = 2;
                }
                break;
            }
        }
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            System.out.println("exit");
            Gdx.app.exit();
        }
     }

//    public void carton() {
//        if (Gdx.input.justTouched()) {
//            // 点击屏幕一次, 播放音效一次
//            sound.play();
//        }
//        /* 使用画笔将纹理绘制在屏幕上 */
//        batch.begin();                // 绘制开始
//        batch.draw(texture, 0, 0);	// 在屏幕左下角绘制纹理
//            // 1.移动
//            if (timer < 3) {
//                spriteMaster.translateX(1200 * Gdx.graphics.getDeltaTime());
//                spriteMaster.rotate(1200 * Gdx.graphics.getDeltaTime());
//            }
//            // 2.翻转/放缩
//            else if (timer > 3 && timer < 5) {
//                //每秒旋转90度
//                spriteMaster.setPosition(780, 240);
//                // spriteMaster.setScale(1, 1);
//                spriteMaster.rotate(90 * Gdx.graphics.getDeltaTime() * 10);
//                spriteMaster.scale(1 * Gdx.graphics.getDeltaTime());
//
//            } else {
//                //舞台跳转
//                spriteMaster.setPosition(1350, 170);
//                spriteMaster.setSize(500, 500);
//                spriteMaster.setScale(1, 1);
//                spriteMaster.setRotation(0);
//            }
//            spriteMaster.draw(batch);
//            batch.end();
//            // 绘制结束
//    }
    public void GoHome(){
        if (Gdx.input.isKeyPressed(Input.Keys.B)) {
           game.setScreen(new HomeScreen(game));
        }
    }
}
